// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package monitor

import (
	"time"

	"yunion.io/x/onecloud/pkg/apis"
)

const (
	SEND_STATE_OK     = "ok"
	SEND_STATE_SILENT = "silent"
	SEND_STATE_SHIELD = "shield"
)

type AlertRecordListInput struct {
	apis.Meta

	apis.ScopedResourceBaseListInput
	apis.EnabledResourceBaseListInput
	apis.StatusStandaloneResourceListInput

	AlertId   string `json:"alert_id"`
	AlertName string `json:"alert_name"`
	Level     string `json:"level"`
	State     string `json:"state"`
	ResType   string `json:"res_type"`
	Alerting  bool   `json:"alerting"`
	ResName   string `json:"res_name"`
	ResId     string `json:"res_id"`
}

type AlertRecordDetails struct {
	SAlertRecord

	apis.StandaloneAnonResourceDetails
	apis.ScopedResourceBaseInfo

	ResNum      int64  `json:"res_num"`
	AlertName   string `json:"alert_name"`
	TriggerTime time.Time
}

type AlertRecordCreateInput struct {
	apis.StandaloneResourceCreateInput

	AlertId string `json:"alert_id"`
	// 报警级别
	Level     string             `json:"level"`
	State     string             `json:"state"`
	SendState string             `json:"send_state"`
	ResType   string             `json:"res_type"`
	EvalData  []*EvalMatch       `json:"eval_data"`
	AlertRule []*AlertRecordRule `json:"alert_rule"`
}

type AlertRecordRule struct {
	Metric          string `json:"metric"`
	Database        string `json:"database"`
	Measurement     string `json:"measurement"`
	MeasurementDesc string `json:"measurement_desc"`
	ResType         string `json:"res_type"`
	Field           string `json:"field"`
	FieldDesc       string `json:"field_desc"`
	// 比较运算符, 比如: >, <, >=, <=
	Comparator string `json:"comparator"`
	// 报警阀值
	Threshold      string    `json:"threshold"`
	ThresholdRange []float64 `json:"threshold_range"`
	Unit           string    `json:"unit"`
	Period         string    `json:"period"`
	AlertDuration  int64     `json:"alert_duration"`
	ConditionType  string    `json:"condition_type"`
	// 静默期
	SilentPeriod string `json:"silent_period"`
	Reducer      string `json:"reducer"`
}

type AlertRecordHistoryAlertData struct {
	ProjectId string `json:"project_id"`
	Project   string `json:"project"`
	DomainId  string `json:"domain_id"`
	Domain    string `json:"domain"`
	ResType   string `json:"res_type"`
	ResNum    int64  `json:"res_num"`
}

func (self AlertRecordHistoryAlertData) GetMetricTags() map[string]string {
	return map[string]string{
		"tenant_id": self.ProjectId,
		"domain_id": self.DomainId,
		"domain":    self.Domain,
		"tenant":    self.Project,
		"res_type":  self.ResType,
	}
}

type AlertRecordHistoryAlert struct {
	Data []AlertRecordHistoryAlertData `json:"data"`
}

// ProjectAlertResourceCountData 报警资源统计数据（按 scope 分类）
type ProjectAlertResourceCountData struct {
	Scope     string `json:"scope"`      // system/domain/project
	DomainId  string `json:"domain_id"`  // 域ID（domain/project scope 时有效）
	Domain    string `json:"domain"`     // 域名称（domain/project scope 时有效）
	ProjectId string `json:"project_id"` // 项目ID（project scope 时有效）
	Project   string `json:"project"`    // 项目名称（project scope 时有效）
	ResCount  int64  `json:"res_count"`  // 报警资源数量
}

// ProjectAlertResourceCount 报警资源统计结果
type ProjectAlertResourceCount struct {
	Data []ProjectAlertResourceCountData `json:"data"`
}

// ProjectAlertResourceCountInput 项目报警资源统计查询输入
type ProjectAlertResourceCountInput struct {
	StartTime time.Time `json:"start_time"`
	EndTime   time.Time `json:"end_time"`
	ResType   string    `json:"res_type"`
	AlertId   string    `json:"alert_id"`
	Scope     string    `json:"scope"`
}
